package com.zscat.mallplus.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zscat.mallplus.annotation.IgnoreAuth;
import com.zscat.mallplus.dms.entity.DmsOrder;
import com.zscat.mallplus.dms.service.IDeviceService;
import com.zscat.mallplus.dms.service.IDeviceTaskService;
import com.zscat.mallplus.dms.service.IDmsOrderService;
import com.zscat.mallplus.dms.service.IMealService;
import com.zscat.mallplus.qsg.QsgDevice;
import com.zscat.mallplus.qsg.QsgDeviceTask;
import com.zscat.mallplus.qsg.QsgMeal;
import com.zscat.mallplus.sys.entity.SysRole;
import com.zscat.mallplus.sys.entity.SysUser;
import com.zscat.mallplus.sys.entity.SysUserRole;
import com.zscat.mallplus.sys.mapper.SysRoleMapper;
import com.zscat.mallplus.sys.mapper.SysUserRoleMapper;
import com.zscat.mallplus.sys.service.ISysUserService;
import com.zscat.mallplus.ums.entity.UmsAdminMember;
import com.zscat.mallplus.ums.service.IUmsAdminMemberService;
import com.zscat.mallplus.utils.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.management.relation.Role;
import java.util.Date;
import java.util.List;

/**
 * 用户管理Controller
 */
@RestController
@Api(tags = "SysUserController", description = "用户管理")
@RequestMapping("/api/dms")
public class SysUserController {

    @Autowired
    private ISysUserService userService;
    @Resource
    private BCryptPasswordEncoder passwordEncoder;
    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private IUmsAdminMemberService adminMemberService;
    @Autowired
    private SysRoleMapper roleMapper;

    @IgnoreAuth
    @GetMapping(value = "/user/login")
    @ApiOperation(value = "生成用户")
    public Object generateTask(String username,String password,Long adminmemberId) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("username",username);
        SysUser user = userService.getOne(qw);
        if (null!=user){
            if (!passwordEncoder.matches(password, user.getPassword())) {
                return new CommonResult().failed("密码错误.");
            }
            UmsAdminMember member = adminMemberService.getById(adminmemberId);
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("admin_id",user.getId());
            List<SysUserRole> roles = userRoleMapper.selectList(queryWrapper);
            if (null!=roles&&roles.size()>0){
                member.setRole(roles.get(0).getRoleId());
                SysRole role = roleMapper.selectById(roles.get(0).getRoleId());
                member.setRoleName(role.getName());
            }
            member.setStoreId(user.getQsgStoreId().intValue());
            adminMemberService.updateById(member);
            return new CommonResult().success(member);
        }
        return new CommonResult().failed();
    }
}
